//
//  TutorialScene.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 11/29/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "TutorialScene.h"
#import "MainGameScene.h"
#import "CalibrateAccelerometer.h"
#import "CalibratePopup.h"
#import "UserInfo.h"

@implementation TutorialScene

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];
    TutorialScene *layer = [TutorialScene node];
    [scene addChild:layer];
    return scene;
}

-(id) init
{
    if (self = [super init])
    {
        mIsMovable = NO;
        
        winSize = [[CCDirector sharedDirector] winSize];
        
        mUserNode = [CCSprite spriteWithSpriteFrameName:SFN_PNODE];
        [mUserNode setPosition:ccp(winSize.width / 2, winSize.height / 2)];
        [mUserNode setColor:CL_PANEL_BTN];
        [self addChild:mUserNode z:50];
        
        mUserBlade = [CCBlade bladeWithMaximumPoint:40];
        [mUserBlade setAutoDim:YES];
        [mUserBlade setTexture:[[CCTextureCache sharedTextureCache] addImage:@"node_tail.png"]];
        mUserBlade.color = ccc4f(mUserNode.color.r * 0.5 / 255.0f, mUserNode.color.g * 0.5 / 255.0f, mUserNode.color.b * 0.5 / 255.0f, 0.5);
        [self addChild:mUserBlade z:-50];
        
//        [[CalibrateAccelerometer getInstance] setType:eAT_TOP_DOWN];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onCalibratePopupClose) name:EVENT_CALIBRATE_POPUP_CLOSE object:nil];
        
        CalibratePopup *popup = [CalibratePopup node];
        [self addChild:popup z:100];
        
    }
    
    return self;
}

-(void) onCalibratePopupClose
{
    [self step1];
    
    self.accelerometerEnabled = YES;
    [self scheduleUpdate];
}

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    if (!mIsMovable)
        return;
    
    [[CalibrateAccelerometer getInstance] calibrate:acceleration];
    mGravity.x = [CalibrateAccelerometer getInstance]._x * SPEED_NODE;
    mGravity.y = [CalibrateAccelerometer getInstance]._y * SPEED_NODE;
}

-(void) update:(ccTime)delta
{
    if (!mIsMovable)
        return;
    
    float newX = [mUserNode position].x + mGravity.x * delta;
    float newY = [mUserNode position].y + mGravity.y * delta;
    
    newX = max(newX, mUserNode.contentSize.width / 2);
    newX = min(newX, [CCDirector sharedDirector].winSize.width - mUserNode.contentSize.width / 2);
    
    newY = max(newY, mUserNode.contentSize.height / 2);
    newY = min(newY, [CCDirector sharedDirector].winSize.height - mUserNode.contentSize.height / 2);
    
    [mUserNode setPosition:ccp(newX, newY)];
    [mUserBlade push:ccp(newX, newY)];
}

-(void) step1 //
{
    CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"This is you" fntFile:FONT_DEFAULT];
    [lb setAnchorPoint:ccp(0.5f, 0.5f)];
    [lb setPosition:ccp(winSize.width / 2, winSize.height - uniPos(70, 140))];
    [lb setOpacity:0];
    [self addChild:lb];
    
    CCDelayTime *delay1 = [CCDelayTime actionWithDuration:1];
    CCFadeTo *fadeIn = [CCFadeTo actionWithDuration:1 opacity:255];
    CCDelayTime *delay2 = [CCDelayTime actionWithDuration:1];
    CCFadeTo *fadeOut = [CCFadeTo actionWithDuration:1 opacity:0];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [lb removeFromParent];
        [self step2];
    }];
    CCSequence *sequence1 = [CCSequence actions:delay1, fadeIn, delay2, fadeOut, callBlock, nil];
    [lb runAction:sequence1];
}

-(void) step2 //
{
    mIsMovable = YES;
    
    CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"Now tilt your device to move around" fntFile:FONT_DEFAULT];
    [lb setAnchorPoint:ccp(0.5f, 0.5f)];
    [lb setPosition:ccp(winSize.width / 2, winSize.height - uniPos(70, 140))];
    [lb setOpacity:0];
    [self addChild:lb];
    
    CCDelayTime *delay1 = [CCDelayTime actionWithDuration:1];
    CCFadeTo *fadeIn = [CCFadeTo actionWithDuration:1 opacity:255];
    CCDelayTime *delay2 = [CCDelayTime actionWithDuration:5];
    CCFadeTo *fadeOut = [CCFadeTo actionWithDuration:1 opacity:0];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [lb removeFromParent];
        [self step3];
    }];
    CCSequence *sequence1 = [CCSequence actions:delay1, fadeIn, delay2, fadeOut, callBlock, nil];
    [lb runAction:sequence1];
}

-(void) step3 //
{
    CCSprite *circle = [CCSprite spriteWithSpriteFrameName:SFN_DOT_CIRCLE];
    [circle setPosition:uniPos(ccp(100, 150), ccp(200, 300))];
    [circle setOpacity:0];
    [self addChild:circle z:-5 tag:15];
    
    CCDelayTime *delay1 = [CCDelayTime actionWithDuration:1];
    CCFadeTo *fadeIn = [CCFadeTo actionWithDuration:1 opacity:255];
    CCSequence *sequence1 = [CCSequence actions:delay1, fadeIn, nil];
    [circle runAction:sequence1];
    
    CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"Move inside this circle" fntFile:FONT_DEFAULT];
    [lb setAnchorPoint:ccp(0.5f, 0.5)];
    [lb setPosition:ccp(circle.position.x, circle.position.y - uniPos(40, 80))];
    [lb setOpacity:0];
    [self addChild:lb z:0 tag:16];
    
    CCDelayTime *delay2 = [CCDelayTime actionWithDuration:1];
    CCFadeTo *fadeto2 = [CCFadeTo actionWithDuration:1 opacity:255];
    CCSequence *sequence2 = [CCSequence actions:delay2, fadeto2, nil];
    [lb runAction:sequence2];
    
    [self schedule:@selector(updateStep3)];
}

-(void) updateStep3
{
    CCSprite *circle = (CCSprite *)[self getChildByTag:15];
    float distance = ccpLength(ccpSub(mUserNode.position, circle.position));
    if (distance <= uniPos(15, 30))
    {
        CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"Great" fntFile:FONT_DEFAULT];
        [lb setAnchorPoint:ccp(0.5f, 0.)];
        [lb setPosition:ccp(circle.position.x, circle.position.y + uniPos(30, 60))];
        [lb setScale:0];
        [self addChild:lb];
        
        CCScaleTo *scaleTo = [CCScaleTo actionWithDuration:0.3 scale:1];
        CCDelayTime *delay = [CCDelayTime actionWithDuration:2];
        CCScaleTo *scaleTo2 = [CCScaleTo actionWithDuration:0.3 scale:0];
        CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
            
            [lb removeFromParent];
        }];
        CCSequence *sequence1 = [CCSequence actions:scaleTo, delay, scaleTo2, callBlock, nil];
        [lb runAction:sequence1];

        CCDelayTime *delay2 = [CCDelayTime actionWithDuration:2.3];
        CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:0.3 opacity:0];
        CCSequence *sequence2 = [CCSequence actions:delay2, fadeTo, nil];
        [circle runAction:sequence2];
        
        CCLabelBMFont *lb2 = (CCLabelBMFont *)[self getChildByTag:16];
        CCDelayTime *delay3 = [CCDelayTime actionWithDuration:2.3];
        CCFadeTo *fadeTo3 = [CCFadeTo actionWithDuration:0.3 opacity:0];
        CCCallBlock *callBlock3 = [CCCallBlock actionWithBlock:^{
            [lb2 removeFromParent];
            [self step4];
        }];
        CCSequence *sequence3 = [CCSequence actions:delay3, fadeTo3, callBlock3, nil];
        [lb2 runAction:sequence3];
        
        [self unschedule:_cmd];
    }
}

-(void) step4 //
{
    CCSprite *circle = (CCSprite *)[self getChildByTag:15];
    [circle setPosition:ccp(winSize.width - 200, winSize.height - 200)];
    
    CCDelayTime *delay1 = [CCDelayTime actionWithDuration:1];
    CCFadeTo *fadeIn = [CCFadeTo actionWithDuration:1 opacity:255];
    
    CCSequence *sequence1 = [CCSequence actions:delay1, fadeIn, nil];
    [circle runAction:sequence1];
    
    CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"Let's try again" fntFile:FONT_DEFAULT];
    [lb setAnchorPoint:ccp(0.5f, 0.5)];
    [lb setPosition:ccp(circle.position.x, circle.position.y - uniPos(40, 80))];
    [lb setOpacity:0];
    [self addChild:lb z:0 tag:16];
    
    CCDelayTime *delay2 = [CCDelayTime actionWithDuration:1];
    CCFadeTo *fadeto2 = [CCFadeTo actionWithDuration:1 opacity:255];
    CCSequence *sequence2 = [CCSequence actions:delay2, fadeto2, nil];
    [lb runAction:sequence2];
    
    [self schedule:@selector(updateStep4)];
}

-(void) updateStep4
{
    CCSprite *circle = (CCSprite *)[self getChildByTag:15];
    float distance = ccpLength(ccpSub(mUserNode.position, circle.position));
    if (distance <= uniPos(15, 30))
    {
        CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"Awesome" fntFile:FONT_DEFAULT];
        [lb setAnchorPoint:ccp(0.5f, 0.)];
        [lb setPosition:ccp(circle.position.x, circle.position.y + uniPos(30, 60))];
        [lb setScale:0];
        [self addChild:lb];
        
        
        CCScaleTo *scaleTo = [CCScaleTo actionWithDuration:0.3 scale:1];
        CCDelayTime *delay = [CCDelayTime actionWithDuration:2];
        CCScaleTo *scaleTo2 = [CCScaleTo actionWithDuration:0.3 scale:0];
        CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
            [lb removeFromParent];
        }];
        CCSequence *sequence1 = [CCSequence actions:scaleTo, delay, scaleTo2, callBlock, nil];
        [lb runAction:sequence1];
        
        CCDelayTime *delay2 = [CCDelayTime actionWithDuration:2.3];
        CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:0.3 opacity:0];
        CCCallBlock *callBlock2 = [CCCallBlock actionWithBlock:^{
            [circle removeFromParent];
        }];
        CCSequence *sequence2 = [CCSequence actions:delay2, fadeTo, callBlock2, nil];
        [circle runAction:sequence2];
        
        CCLabelBMFont *lb2 = (CCLabelBMFont *)[self getChildByTag:16];
        CCDelayTime *delay3 = [CCDelayTime actionWithDuration:2.3];
        CCFadeTo *fadeTo3 = [CCFadeTo actionWithDuration:0.3 opacity:0];
        CCCallBlock *callBlock3 = [CCCallBlock actionWithBlock:^{
            [lb2 removeFromParent];
            [self step5];
        }];
        CCSequence *sequence3 = [CCSequence actions:delay3, fadeTo3, callBlock3, nil];
        [lb2 runAction:sequence3];
        
        [self unschedule:_cmd];
    }
}

-(void) step5 //
{
    PNode *node = [[[PNode alloc] initWithType:eNT_PATH_NODE] autorelease];
    [node setPosition:ccp(200, 200)];
    [node setScale:0];
    [self addChild:node z:0 tag:30];
    
    CCScaleTo *scaleTo = [CCScaleTo actionWithDuration:1 scale:1];
    [node runAction:scaleTo];
    
    PNodeIndicator *nodeI = [[[PNodeIndicator alloc] init] autorelease];
    [nodeI setPosition:node.position];
    [nodeI setScale:0];
    [self addChild:nodeI z:0 tag:31];
    
    CCScaleTo *scaleTo2 = [CCScaleTo actionWithDuration:1 scale:1];
    [nodeI runAction:scaleTo2];
    
    CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"Collide with this one" fntFile:FONT_DEFAULT];
    [lb setAnchorPoint:ccp(0.5f, 0.)];
    [lb setPosition:ccp(node.position.x, node.position.y - uniPos(50, 100))];
    [lb setOpacity:0];
    [self addChild:lb z:0 tag:15];
    
    CCFadeTo *fadeto2 = [CCFadeTo actionWithDuration:1 opacity:255];
    [lb runAction:fadeto2];
    
    [self schedule:@selector(updateStep5)];
}

-(void) updateStep5
{
    PNode *node = (PNode *) [self getChildByTag:30];
    float distance = ccpLength(ccpSub(mUserNode.position, node.position));
    if (distance <= mUserNode.contentSize.width)
    {
        PNodeIndicator *nodeI = (PNodeIndicator *) [self getChildByTag:31];
        [nodeI removeFromParent];
        [node setColor:CL_PANEL_BTN];
        
        CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"Cool" fntFile:FONT_DEFAULT];
        [lb setAnchorPoint:ccp(0.5f, 0.)];
        [lb setPosition:ccp(node.position.x, node.position.y + uniPos(30, 60))];
        [lb setScale:0];
        [self addChild:lb];
        
        CCScaleTo *scaleTo = [CCScaleTo actionWithDuration:0.3 scale:1];
        CCDelayTime *delay = [CCDelayTime actionWithDuration:2];
        CCScaleTo *scaleTo2 = [CCScaleTo actionWithDuration:0.3 scale:0];
        CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
            
            [lb removeFromParent];
            [self step6];
        }];
        CCSequence *sequence1 = [CCSequence actions:scaleTo, delay, scaleTo2, callBlock, nil];
        [lb runAction:sequence1];
        
        CCLabelBMFont *lb2 = (CCLabelBMFont *)[self getChildByTag:15];
        CCDelayTime *delay7 = [CCDelayTime actionWithDuration:2.3];
        CCFadeTo *fadeTo7 = [CCFadeTo actionWithDuration:1 opacity:0];
        CCCallBlock *callBlock7 = [CCCallBlock actionWithBlock:^{
            [lb2 removeFromParent];
        }];
        CCSequence *sequence7 = [CCSequence actions:delay7, fadeTo7, callBlock7, nil];
        [lb2 runAction:sequence7];
        
        CCDelayTime *delay2 = [CCDelayTime actionWithDuration:2.3];
        CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:0.3 opacity:0];
        CCCallBlock *callBlock2 = [CCCallBlock actionWithBlock:^{
            [node removeFromParent];
            
        }];
        CCSequence *sequence2 = [CCSequence actions:delay2, fadeTo, callBlock2, nil];
        [node runAction:sequence2];
        
        [self unschedule:_cmd];
    }
}

-(void) step6 //
{
    PNode *node1 = [[[PNode alloc] initWithType:eNT_PATH_NODE] autorelease];
    [node1 setPosition:ccp(winSize.width / 2 - uniPos(100, 200), winSize.height / 2)];
    [node1 setScale:0];
    [self addChild:node1 z:0 tag:30];
    
    CCScaleTo *scaleTo = [CCScaleTo actionWithDuration:1 scale:1];
    [node1 runAction:scaleTo];
 
    PNodeIndicator *nodeI = [[[PNodeIndicator alloc] init] autorelease];
    [nodeI setPosition:node1.position];
    [nodeI setScale:0];
    [self addChild:nodeI z:0 tag:31];
    
    CCScaleTo *scaleTo2 = [CCScaleTo actionWithDuration:1 scale:1];
    [nodeI runAction:scaleTo2];
    
    PNode *node2 = [[[PNode alloc] initWithType:eNT_PATH_NODE] autorelease];
    [node2 setPosition:ccp(winSize.width / 2 + uniPos(100, 200), winSize.height / 2)];
    [node2 setScale:0];
    node1._nextNode = node2;
    [self addChild:node2 z:0 tag:40];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:1];
    CCScaleTo *scaleTo3 = [CCScaleTo actionWithDuration:1 scale:1];
    CCSequence *sequence = [CCSequence actions:delay, scaleTo3, nil];
    [node2 runAction:sequence];
    
    PLine *line = [[[PLine alloc] initWithNode:node1] autorelease];
    [line setOpacity:0];
    [self addChild:line z:-5 tag:50];
    
    CCDelayTime *delay2 = [CCDelayTime actionWithDuration:1];
    CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:0.5 opacity:255];
    CCSequence *sequence2 = [CCSequence actions:delay2, fadeTo, nil];
    [line runAction:sequence2];
    
    PSmallLight *smallLight = [[[PSmallLight alloc] initWithLine:line] autorelease];
    [smallLight setOpacity:0];
    [smallLight setColor:CL_PANEL_BTN];
    [self addChild:smallLight z:5 tag:60];
    
    CCDelayTime *delay5 = [CCDelayTime actionWithDuration:2];
    CCCallBlock *callBlock5 = [CCCallBlock actionWithBlock:^{
        [smallLight startAction];
        [self schedule:@selector(updateStep6)];
    }];
    CCSequence *sequence5 = [CCSequence actions:delay5, callBlock5, nil];
    [smallLight runAction:sequence5];
    
    CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"This node first" fntFile:FONT_DEFAULT];
    [lb setAnchorPoint:ccp(0.5f, 0.5)];
    [lb setPosition:ccp(node1.position.x, node1.position.y - uniPos(40, 80))];
    [lb setOpacity:0];
    [self addChild:lb z:0 tag:15];
    
    CCDelayTime *delay6 = [CCDelayTime actionWithDuration:1];
    CCFadeTo *fadeto6 = [CCFadeTo actionWithDuration:1 opacity:255];
    CCSequence *sequence6 = [CCSequence actions:delay6, fadeto6, nil];
    [lb runAction:sequence6];
}

-(void) updateStep6
{
    PNode *node1 = (PNode *)[self getChildByTag:30];
    float distance = ccpLength(ccpSub(mUserNode.position, node1.position));
    if (distance <= mUserNode.contentSize.width)
    {
        PNodeIndicator *nodeI = (PNodeIndicator *) [self getChildByTag:31];
        PNode *node2 = (PNode *)[self getChildByTag:40];
        
        [nodeI setPosition:node2.position];
        [node1 setColor:CL_PANEL_BTN];
        
        CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"Then this one" fntFile:FONT_DEFAULT];
        [lb setAnchorPoint:ccp(0.5f, 0.5)];
        [lb setPosition:ccp(node2.position.x, node2.position.y - uniPos(40, 80))];
        [lb setOpacity:0];
        [self addChild:lb z:0 tag:16];
        
        CCFadeTo *fadeto2 = [CCFadeTo actionWithDuration:1 opacity:255];
        [lb runAction:fadeto2];
        
        [self unschedule:_cmd];
        [self schedule:@selector(updateStep6_2)];
    }
}

-(void) updateStep6_2
{
    PNode *node2 = (PNode *)[self getChildByTag:40];
    float distance = ccpLength(ccpSub(mUserNode.position, node2.position));
    if (distance <= mUserNode.contentSize.width)
    {
        mIsMovable = NO;
        
        PNodeIndicator *nodeI = (PNodeIndicator *) [self getChildByTag:31];
        PNode *node1 = (PNode *)[self getChildByTag:30];
        PLine *line = (PLine *)[self getChildByTag:50];
        PSmallLight *smallLight = (PSmallLight *)[self getChildByTag:60];
        
        [nodeI removeFromParent];
        [node2 setColor:CL_PANEL_BTN];
        [line setLineState:eLS_CONTECTED];
        [smallLight removeFromParent];
        
        
        CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"Perfect" fntFile:FONT_DEFAULT];
        [lb setAnchorPoint:ccp(0.5f, 0.)];
        [lb setPosition:ccp(node2.position.x, node2.position.y + uniPos(30, 60))];
        [lb setScale:0];
        [self addChild:lb];
        
        CCScaleTo *scaleTo = [CCScaleTo actionWithDuration:0.3 scale:1];
        CCDelayTime *delay = [CCDelayTime actionWithDuration:2];
        CCScaleTo *scaleTo2 = [CCScaleTo actionWithDuration:0.3 scale:0];
        CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
            [lb removeFromParent];
            [line removeFromParent];
        }];
        CCSequence *sequence1 = [CCSequence actions:scaleTo, delay, scaleTo2, callBlock, nil];
        [lb runAction:sequence1];
        
        CCLabelBMFont *lb2 = (CCLabelBMFont *)[self getChildByTag:15];
        CCDelayTime *delay7 = [CCDelayTime actionWithDuration:2.3];
        CCFadeTo *fadeTo7 = [CCFadeTo actionWithDuration:1 opacity:0];
        CCCallBlock *callBlock7 = [CCCallBlock actionWithBlock:^{
            [lb2 removeFromParent];
        }];
        CCSequence *sequence7 = [CCSequence actions:delay7, fadeTo7, callBlock7, nil];
        [lb2 runAction:sequence7];
        
        CCLabelBMFont *lb3 = (CCLabelBMFont *)[self getChildByTag:16];
        CCDelayTime *delay8 = [CCDelayTime actionWithDuration:2.3];
        CCFadeTo *fadeTo8 = [CCFadeTo actionWithDuration:1 opacity:0];
        CCCallBlock *callBlock8 = [CCCallBlock actionWithBlock:^{
            [lb3 removeFromParent];
        }];
        CCSequence *sequence8 = [CCSequence actions:delay8, fadeTo8, callBlock8, nil];
        [lb3 runAction:sequence8];
        
        CCDelayTime *delay3 = [CCDelayTime actionWithDuration:2.3];
        CCScaleTo *scaleTo3 = [CCScaleTo actionWithDuration:1 scale:0];
        CCCallBlock *callBlock3 = [CCCallBlock actionWithBlock:^{
            [node1 removeFromParent];
        }];
        CCSequence *sequence3 = [CCSequence actions:delay3, scaleTo3, callBlock3, nil];
        [node1 runAction:sequence3];
        
        CCDelayTime *delay4 = [CCDelayTime actionWithDuration:2.3];
        CCScaleTo *scaleTo4 = [CCScaleTo actionWithDuration:1 scale:0];
        CCCallBlock *callBlock4 = [CCCallBlock actionWithBlock:^{
            [node2 removeFromParent];
            [self step7];
        }];
        CCSequence *sequence4 = [CCSequence actions:delay4, scaleTo4, callBlock4, nil];
        [node2 runAction:sequence4];
        
        [self unschedule:_cmd];
    }
}

-(void) step7 //
{
    CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"Tutorial complete!!!" fntFile:FONT_DEFAULT];
    [lb setAnchorPoint:ccp(0.5f, 0.5)];
    [lb setPosition:ccp(winSize.width / 2, winSize.height / 2)];
    [lb setOpacity:0];
    [self addChild:lb];
    
    CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:1 opacity:255];
    CCDelayTime *delayTime = [CCDelayTime actionWithDuration:3];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [LevelManager getInstance]._currentLevel = 0;
        [[CCDirector sharedDirector] replaceScene:[MainGameScene scene]];
    }];
    CCSequence *sequence = [CCSequence actions:fadeTo, delayTime, callBlock, nil];
    [lb runAction:sequence];
    
    
    //
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    if ([defaults objectForKey:KEY_FIRST_TIME_PLAY] == nil)
    {
        [UserInfo getInstance]._numItemGlove++;
        [UserInfo getInstance]._numItemMagnet++;
        [UserInfo getInstance]._numItemTime++;
        [UserInfo getInstance]._numItemXray++;
        [[UserInfo getInstance] saveUserInfo];
    }
    
    [defaults setBool:YES forKey:KEY_FIRST_TIME_PLAY];
    [defaults synchronize];
    
    PhysicSystem::getInstance()->destroyAllBody();
}

-(void) dealloc
{
    CCLOG(@"Tutorial Scene dealloc!");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

@end
